﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Management
{
    public partial class Database : Form
    {
        public Database()
        {
            InitializeComponent();
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var search = textBox1.Text;
            string cmdString =string.Format( "select * from Articles where Title like '%{0}%'",search);
            var res = DbHelper.GetDataTable(cmdString);

            dataGridView1.DataSource = res;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();

            if (res.Equals(DialogResult.Yes))
            {
                string cmdSting = "select * from Articles";
                var data = DbHelper.GetDataTable(cmdSting);
                dataGridView1.DataSource = data;
            }
        }
        //更新
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;


                EditForm editForm = new EditForm(id, title, author);


                var res = editForm.ShowDialog();

                if (res.Equals(DialogResult.Yes))
                {
                    string cmdSting = "select * from Articles";
                    var data = DbHelper.GetDataTable(cmdSting);
                    dataGridView1.DataSource = data;
                }
            }
            else
            {
                MessageBox.Show("你还没有选择呢！", "提示");
            }
        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count>0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = row.Cells["Id"].Value;

                var res = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    string cmdString = string.Format("delete Articles where Id = {0}",id);
                    var del = DbHelper.AddOrUpdateOrDelete(cmdString);
                    if(del==1)
                    {
                        MessageBox.Show("删除成功咯！", "提示");
                        string select = "select * from Articles";
                        var show = DbHelper.GetDataTable(select);
                        dataGridView1.DataSource = show;
                    }
                }
                else
                {
                    this.Close();
                }
                    
            }
            else
            {
                MessageBox.Show("你还没有选中呢！", "提示");
            }
            
        }

        private void Database_Load(object sender, EventArgs e)
        {
            string cmdString = "select * from Articles";
            var res = DbHelper.GetDataTable(cmdString);

            dataGridView1.DataSource = res;

            //属性
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
    }
}
